const api = getApp().globalData.api
Page({
    data: {
        result: {},
        show: false,
        actions: [
            {
                name: '删除',
                subname: '该操作是不可逆的，是否确认？',
            },
        ],
    },
    detail: async function (id) {
        let res = await api.curlPost('?cla=equipment&fun=detail', {id: id})
        if (res.code === 1) {
            this.setData({result: res.data.result})
        } else {
            api.showToast(res.warn)
            wx.navigateBack({delta: 1})
        }
    },
    onLoad: async function (options) {
      const id = options.id ? options.id : options.scene
      if (!id) {
          api.showToast('参数错误')
          wx.navigateTo({
              url: 'index'
          })
      } else {
          this.detail(id)
      }
    },
    // 修改
    onEdit: async function (obj) {
        let data = obj.currentTarget.dataset
        if (!data.id) {
            api.showToast('参数错误')
        } else {
            wx.navigateTo({
                url: 'add?id=' + data.id
            })
        }
    },
    // 修改状态
    changeState: async function (obj) {
        let data = obj.currentTarget.dataset
        if (!data.id) {
            api.showToast('参数错误')
        } else {
            let res = await api.curlPost('?cla=equipment&fun=changeState', {id: data.id, state: data.state})
            if (res.code === 1) {
                this.detail(data.id)
            }
            api.showToast(res.warn)
        }
    },
    // 执行删除操作
    onSelect: async function (obj) {
        let data = obj.currentTarget.dataset
        if (!data.id) {
            api.showToast('参数错误')
        } else {
            let res = await api.curlPost('?cla=equipment&fun=delete', {id: data.id})
            if (res.code === 1) {
                wx.navigateTo({
                    url: 'index'
                })
            }
            api.showToast(res.warn)
        }
    },
    onClose() {
        this.setData({show: false});
    },
    onShowOpt() {
        this.setData({show: true});
    },
    // 巡检
    inspection: async function (obj) {
      let data = obj.currentTarget.dataset
      if (!data.id) {
          api.showToast('参数错误')
      } else {
          wx.navigateTo({
              url: '../inspection/edit?eqid=' + data.id
          })
      }
    },
});